import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";

declare var $:any;
@Component({
  selector: 'sa-optimize-yt',
  templateUrl: './optimize-yt.component.html',
})
export class optimizeYtComponent implements OnInit {
	validatorOptions = {
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			fileToUpload: {
				validators: {
					notEmpty: {
						message: '请上传文件'
					}
				}
			}
		}

	}
	deviceId = '';
	fileToUpload: File = null;
	fileName='选择需要上传的csv文件';
	format:any='';
	resultMessage='';
	
	constructor(private router: Router, private activatedRoute: ActivatedRoute,private jsonApiService: JsonApiService) {
		activatedRoute.queryParams.subscribe(queryParams => {
//			this.resourceId = queryParams.id;
//			this.pageType = queryParams.type;
		});
	}
	uploadFile(){
		let postParam = new FormData();
        postParam.append('file', this.fileToUpload);
        if(!this.fileToUpload){
     		this.jsonApiService.message('没有文件,不允许执行')
        	return false;
        }
        this.jsonApiService.form_http("system/uploadweightbeautycsvfile.html", postParam).subscribe(data => {
        	this.jsonApiService.message(data.message);
			this.resultMessage=data.message;
		})
    }
	onSubmit() {
		$('#pathCorrectionFrom').bootstrapValidator('validate'); //开启验证
		let isValid = $('#pathCorrectionFrom').data('bootstrapValidator').isValid();  //验证是否通过
		if(isValid){
			this.uploadFile();
		}
	}
	fileChange(files: FileList) {
		if(!files.item(0)){
			this.fileToUpload = null;
			this.fileName='选择需要上传的csv文件';
			this.jsonApiService.message('选择需要上传的csv文件')
			return
		}
		if(files[0].type!=='text/csv'){
			this.fileToUpload = null;
			this.jsonApiService.message('请选择csv格式文件')
			return;
		}else{
			this.fileToUpload = files.item(0);
			this.fileName=files.item(0).name;
		}
		if(!this.fileToUpload){
			this.jsonApiService.message('选择需要上传的csv文件')
			this.fileName='选择需要上传的csv文件';
			return false;
		}
    }
  	ngOnInit() {}
}